package fivestar.ui.main;
import java.awt.BorderLayout;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

import org.jdesktop.swingx.JXPanel;
import org.jdesktop.swingx.painter.ImagePainter;
import org.pushingpixels.flamingo.api.ribbon.JRibbonFrame;
 
public class Theme extends javax.swing.JDialog implements ActionListener { 
	private static final long serialVersionUID = 2444048830833517379L;
	private JPanel pnlTheme;
	private JButton btnAccept;
	private JButton btnCancel;
	private JRadioButton rbtOfficeSilver;
	private JRadioButton rbtOfficeBlue;
	private JRadioButton rbtOfficeBlack;
	private JRadioButton rbtCreme;
	private JRadioButton rbtBusinessBlue;
	private JRadioButton rbtBusinessBlack;
	private JXPanel pnlPreviousImage;
    private String temaSeleccionado = "org.pushingpixels.substance.api.skin.SubstanceBusinessBlackSteelLookAndFeel";
    private Frame padre;
    private ButtonGroup groupTheme;
    
	public Theme(Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        padre = parent;
        this.setLocationRelativeTo(null);
        try {
            BufferedImage img = ImageIO.read(MainMenu.class.getResourceAsStream("/fivestar/ui/images/BusinessBlack.png"));
            pnlPreviousImage.setBackgroundPainter(new ImagePainter(img) );
        } catch (Exception e) {
            e.printStackTrace();
        }
	}
	
	private void initComponents() {
		try {

			pnlTheme = new JPanel();
			getContentPane().add(pnlTheme, BorderLayout.CENTER);
			pnlTheme.setLayout(null);
			
			pnlPreviousImage = new JXPanel();
			pnlPreviousImage.setBounds(231, 11, 477, 326);
			pnlTheme.add(pnlPreviousImage);

			rbtBusinessBlack = new JRadioButton();
			rbtBusinessBlack.setSelected(true);
			rbtBusinessBlack.addActionListener(this);
			pnlTheme.add(rbtBusinessBlack);
			rbtBusinessBlack.setText("Business Black");
			rbtBusinessBlack.setBounds(44, 57, 164, 23);

			rbtBusinessBlue = new JRadioButton();
			rbtBusinessBlue.addActionListener(this);
			pnlTheme.add(rbtBusinessBlue);
			rbtBusinessBlue.setText("Business Blue");
			rbtBusinessBlue.setBounds(44, 84, 164, 23);

			rbtCreme = new JRadioButton();
			rbtCreme.addActionListener(this);
			pnlTheme.add(rbtCreme);
			rbtCreme.setText("Creme");
			rbtCreme.setBounds(44, 113, 164, 23);

			rbtOfficeBlack = new JRadioButton();
			rbtOfficeBlack.addActionListener(this);
			pnlTheme.add(rbtOfficeBlack);
			rbtOfficeBlack.setText("Office Black");
			rbtOfficeBlack.setBounds(44, 140, 164, 23);

			rbtOfficeBlue = new JRadioButton();
			rbtOfficeBlue.addActionListener(this);
			pnlTheme.add(rbtOfficeBlue);
			rbtOfficeBlue.setText("Office Blue");
			rbtOfficeBlue.setBounds(44, 167, 164, 23);

			rbtOfficeSilver = new JRadioButton();
			rbtOfficeSilver.addActionListener(this);
			pnlTheme.add(rbtOfficeSilver);
			rbtOfficeSilver.setText("Office Silver");
			rbtOfficeSilver.setBounds(44, 194, 164, 23);

			btnAccept = new JButton();
			btnAccept.addActionListener(this);
			pnlTheme.add(btnAccept);
			btnAccept.setText("Accept");
			btnAccept.setBounds(44, 237, 80, 23);

			btnCancel = new JButton();
			btnCancel.addActionListener(this);
			pnlTheme.add(btnCancel);
			btnCancel.setText("Cancel");
			btnCancel.setBounds(128, 237, 80, 23);
			
			groupTheme = new ButtonGroup();
	        groupTheme.add(rbtBusinessBlack);
	        groupTheme.add(rbtCreme);
	        groupTheme.add(rbtBusinessBlue);
	        groupTheme.add(rbtOfficeBlack);
	        groupTheme.add(rbtOfficeBlue);
	        groupTheme.add(rbtOfficeSilver);

			this.setSize(738, 393);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
 
	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == btnAccept){
		    try {
                JRibbonFrame.setDefaultLookAndFeelDecorated(true);
                JDialog.setDefaultLookAndFeelDecorated(true);
                ReaderProperties lector = null;
                try {
                    lector = new ReaderProperties();
                } catch (IOException ex) {
                    // Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
                }
                    lector.ubicarPropiedad("Skin", temaSeleccionado) ;
                    UIManager.setLookAndFeel(temaSeleccionado);
                    SwingUtilities.updateComponentTreeUI(padre);
                    this.dispose();
            } catch (Exception unsupportedLookAndFeelException) {
                //todo Capturar mensaje
            }
		}
		if (e.getSource() == btnCancel){
			this.dispose();
		}
		if (e.getSource() == rbtBusinessBlack){
		    try {
	            BufferedImage img = ImageIO.read(MainMenu.class.getResourceAsStream("/fivestar/ui/images/BusinessBlack.png"));
	            pnlPreviousImage.setBackgroundPainter(new ImagePainter(img) );
	            temaSeleccionado = "org.pushingpixels.substance.api.skin.SubstanceBusinessBlackSteelLookAndFeel";
	        } catch (Exception ex) {
	            ex.printStackTrace();
	        }
		}
		if (e.getSource() == rbtBusinessBlue){
			 try {
		            BufferedImage img = ImageIO.read(Main.class.getResourceAsStream("/fivestar/ui/images/BusinessBlue.png"));
		            pnlPreviousImage.setBackgroundPainter(new ImagePainter(img) );
		            temaSeleccionado = "org.pushingpixels.substance.api.skin.SubstanceBusinessBlueSteelLookAndFeel";
		        } catch (Exception ex) {
		            ex.printStackTrace();
		        }
		}
		if (e.getSource() == rbtCreme){
			try {
	            BufferedImage img = ImageIO.read(MainMenu.class.getResourceAsStream("/fivestar/ui/images/Creme.png"));
	            pnlPreviousImage.setBackgroundPainter(new ImagePainter(img) );
	            temaSeleccionado = "org.pushingpixels.substance.api.skin.SubstanceCremeLookAndFeel";
	        } catch (Exception ex) {
	            ex.printStackTrace();
	        }
		}
		if (e.getSource() == rbtOfficeBlack){
			 try {
		            BufferedImage img = ImageIO.read(MainMenu.class.getResourceAsStream("/fivestar/ui/images/OfficeBlack.png"));
		            pnlPreviousImage.setBackgroundPainter(new ImagePainter(img) );
		            temaSeleccionado = "org.pushingpixels.substance.api.skin.SubstanceOfficeBlack2007LookAndFeel";
		        } catch (Exception ex) {
		            ex.printStackTrace();
		        }
		}
		if (e.getSource() == rbtOfficeBlue){
		     try {
		            BufferedImage img = ImageIO.read(MainMenu.class.getResourceAsStream("/fivestar/ui/images/OfficeBlue.png"));
		            pnlPreviousImage.setBackgroundPainter(new ImagePainter(img) );
		            temaSeleccionado = "org.pushingpixels.substance.api.skin.SubstanceOfficeBlue2007LookAndFeel";
		        } catch (Exception ex) {
		            ex.printStackTrace();
		        }
		}
		if (e.getSource() == rbtOfficeSilver){
			try {
	            BufferedImage img = ImageIO.read(MainMenu.class.getResourceAsStream("/fivestar/ui/images/OfficeSilver.png"));
	            pnlPreviousImage.setBackgroundPainter(new ImagePainter(img) );
	            temaSeleccionado = "org.pushingpixels.substance.api.skin.SubstanceOfficeSilver2007LookAndFeel";
	        } catch (Exception ex) {
	            ex.printStackTrace();
	        }
		}
		
	}

	  public static void main(String args[]) {
	        java.awt.EventQueue.invokeLater(new Runnable() {
	            public void run() {
	                Theme dialog = new Theme(new javax.swing.JFrame(), true);
	                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
	                    public void windowClosing(java.awt.event.WindowEvent e) {
	                        System.exit(0);
	                    }
	                });
	                dialog.setVisible(true);
	            }
	        });
	    }
}
